<?php
/**
* Django's templating engine (partial) PHP implementation
* @link http://djangoproject.com Django
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* data context for template rendering
* @uses dataPath
* @author J.Ducastel <jeremie@ducastel.name>
*/
class djtplContext {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	* constructor alias
	* @access
	*/
	function djtplContext(&$data) {
		$this->__construct($data);
	}/**/

	/**
	* "real" constructor
	* @access public
	* @param mixed $data array or object
	*/
	function __construct(&$data) {
		$this->setDataSource($data);
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* sets root data source
	* @param array $data
	* @access public
	* @return bool
	*/
	function setDataSource(&$data) {
		$this->data=&$data;
		return true;
	}/**/

	/**
	* gets data at targeted path
	* @param string $path
	* @access public
	* @return mixed
	*/
	function getPath($path) {
		// searching for alias
		// echo "<p>getting path $path</p>";
		/*if (isset($this->aliases[$path])) {
			echo '<p>'.$path.'='.$this->aliases[$path].'</p>';
			$path=$this->aliases[$path];
		}*/
		return datapath::get($this->data,$path);
	}/**/

	/**
	* sets an alias for some path
	* @param string $path
	* @param string $alias
	* @access public
	* @return bool
	*/
	function setPathAlias($path, $alias) {
		//echo "<p>setting $alias as alias for $path</p>";
		//$this->aliases[$alias]=$path;
		//echo '<pre>'; print_r($this->data);
		// getting a reference on $path target
		$target=&datapath::get($this->data,$path); // print_r($target);
		// setting $alias as a reference on it
		datapath::setRef($this->data,$target,$alias);
		// echo "<p>$alias=$path="; print_r(datapath::get($this->data,$path));
		//echo wakdebug::vardump($this->data);
		return true;
	}/**/

	/**
	* return children keys at a path
	* @param string $path
	* @access public
	* @return array
	*/
	function getKeys($path) {
		return datapath::getChildKeys($this->data,$path);
	}/**/

	/**
	* Sets value for a path
	* @param string $path
	* @param mixed value
	* @access public
	* @return bool
	*/
	function setPath($path,$value) {
		return datapath::set($this->data,$value,$path);
	}/**/

	/**
	* @var array source data reference
	*/
	var $data;

	/**
	 * @var array path aliases
	 */
	//var $aliases=array();
}/* end of djtplContext class */
?>